home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / MDI / Document.pas next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  6.2 KB  |  224 lines

  1. unit Document;
  2.  
  3.  
  4. interface
  5.  
  6. uses
  7.   System.Drawing,
  8.   System.Drawing.Text,
  9.   System.ComponentModel,
  10.   System.Windows.Forms;
  11.  
  12. type
  13.   TForm           = System.Windows.Forms.Form;
  14.   TMainMenu       = System.Windows.Forms.MainMenu;
  15.   TMenuItem       = System.Windows.Forms.MenuItem;
  16.   TContainer      = System.ComponentModel.Container;
  17.   TRichTextBox    = System.Windows.Forms.RichTextBox;
  18.   TSize           = System.Drawing.Size;
  19.   TFont           = System.Drawing.Font;
  20.   TFontFamily     = System.Drawing.FontFamily;
  21.   TOpenFileDialog = OpenFileDialog;
  22.  
  23.   TFontSizes   = (Small, Medium, Large);
  24. const
  25.   FontSizes: array[Low(TFontSizes)..High(TFontSizes)] of Single = (8, 12, 24);
  26.   a: Integer = 5;
  27.  
  28. type
  29.   TDocument = class(TForm)
  30.   private
  31.     FComponents: TContainer;
  32.     FFontSize: Single;
  33.     procedure InitComponents;
  34.     procedure LoadDocumentClicked(Sender: TObject; Args: EventArgs);
  35.     procedure FormatFontClicked(Sender: TObject; Args: EventArgs);
  36.     procedure FormatSizeClicked(Sender: TObject; Args: EventArgs);
  37.   published
  38.     MainMenu: TMainMenu;
  39.     RichTextBox: TRichTextBox;
  40.  
  41.     miFormatFontChecked: TMenuItem;
  42.     miFormatSizeChecked: TMenuItem;
  43.  
  44.     miSmall: TMenuItem;
  45.     miMedium: TMenuItem;
  46.     miLarge: TMenuItem;
  47.  
  48.     miSansSerif: TMenuItem;
  49.     miSerif: TMenuItem;
  50.     miMonoSpace: TMenuItem;
  51.  
  52.     currentFontFamily: FontFamily;
  53.     monoSpaceFontFamily: FontFamily;
  54.     sansSerifFontFamily: FontFamily;
  55.     serifFontFamily: FontFamily;    
  56.   public
  57.     constructor Create(Caption: string);
  58.   end;
  59.  
  60. implementation
  61.  
  62. { TDocument }
  63.  
  64.  
  65. const
  66.   b: Integer = 5;
  67.  
  68. constructor TDocument.Create(Caption: string);
  69. var
  70.   miFile, miFormat, miFontFace, miFontSize: TMenuItem;
  71. begin
  72.   inherited Create;
  73.   InitComponents;
  74.  
  75.   Text:= Caption;
  76.  
  77.   // Initialize Fonts - use generic fonts to avoid problems across
  78.   // different versions of the OS
  79.   monoSpaceFontFamily:= TFontFamily.Create(GenericFontFamilies.Monospace);
  80.   sansSerifFontFamily:= TFontFamily.Create(GenericFontFamilies.SansSerif);
  81.   serifFontFamily:= TFontFamily.Create(GenericFontFamilies.Serif);
  82.   currentFontFamily:= sansSerifFontFamily;
  83.  
  84.   RichTextBox.Font:= TFont.Create(currentFontFamily, FFontSize);
  85.   RichTextBox.Text:= Caption;
  86.  
  87.   //Add File Menu
  88.   miFile:= mainMenu.MenuItems.Add('&File');
  89.   miFile.MergeType:= MenuMerge.MergeItems;
  90.   miFile.MergeOrder:= 0;
  91.  
  92.   miFile.MenuItems.Add(TMenuItem.Create('&Load Document (' + Caption + ')', nil, Shortcut.CtrlL));
  93.   miFile.MenuItems[0].Add_Click(LoadDocumentClicked);
  94.   miFile.MenuItems[0].MergeOrder:= 105;
  95.  
  96.   //Add Formatting Menu
  97.   miFormat:= TMenuItem.Create('F&ormat (' + Caption + ')', nil, Shortcut.CtrlO);
  98.   miFormat.MergeType:= MenuMerge.Add;
  99.   miFormat.MergeOrder:= 5;
  100.   mainMenu.MenuItems.Add(miFormat);
  101.  
  102.   //Font Face sub-menu
  103.   miSansSerif:= TMenuItem.Create;
  104.   miSansSerif.Text:= '&1.' + sansSerifFontFamily.Name;
  105.   miSansSerif.Add_Click(FormatFontClicked);
  106.  
  107.   miSerif:= TMenuItem.Create;
  108.   miSerif.Text:='&2.' + serifFontFamily.Name;
  109.   miSerif.Add_Click(FormatFontClicked);
  110.  
  111.   miMonoSpace:= TMenuItem.Create;
  112.   miMonoSpace.Text:= '&3.' + monoSpaceFontFamily.Name;
  113.   miMonoSpace.Add_Click(FormatFontClicked);
  114.  
  115.   miSansSerif.Checked:= True ;
  116.   miFormatFontChecked:= miSansSerif ;
  117.   miSansSerif.DefaultItem:= True ;
  118.  
  119.   miFontFace:= TMenuItem.Create;
  120.   miFontFace.Text:= 'Font &Face';
  121.   miFontFace.MenuItems.Add(miSansSerif);
  122.   miFontFace.MenuItems.Add(miSerif);
  123.   miFontFace.MenuItems.Add(miMonoSpace);
  124.  
  125.   miFormat.MenuItems.Add(miFontFace);
  126.  
  127.   //Font Size sub-menu
  128.   miSmall:= TMenuItem.Create;
  129.   miSmall.Text:= '&Small';
  130.   miSmall.Add_Click(FormatSizeClicked);
  131.  
  132.   miMedium:= TMenuItem.Create;
  133.   miMedium.Text:= '&Medium';
  134.   miMedium.Add_Click(FormatSizeClicked);
  135.  
  136.   miLarge:= TMenuItem.Create;
  137.   miLarge.Text:= '&Large';
  138.   miLarge.Add_Click(FormatSizeClicked);
  139.  
  140.   miMedium.Checked:= True;
  141.   miMedium.DefaultItem:= True;
  142.   miFormatSizeChecked:= miMedium;
  143.  
  144.   miFontSize:= TMenuItem.Create;
  145.   miFontSize.Text:= 'Font &Size';
  146.   miFontSize.MenuItems.Add(miSmall);
  147.   miFontSize.MenuItems.Add(miMedium);
  148.   miFontSize.MenuItems.Add(miLarge);
  149.   miFormat.MenuItems.Add(miFontSize);
  150. end;
  151.  
  152. procedure TDocument.LoadDocumentClicked(Sender: TObject; Args: EventArgs);
  153. var
  154.   OpenDlg: TOpenFileDialog;
  155. begin
  156.   // Initialize the OpenFileDialog to look for RTF files.
  157.   OpenDlg:= TOpenFileDialog.Create;
  158.   OpenDlg.DefaultExt:= '*.txt';
  159.   OpenDlg.Filter:= 'Text Files|*.txt';
  160.  
  161.   // Determine whether the user selected a file from the OpenFileDialog.
  162.   if ( OpenDlg.ShowDialog = DialogResult.OK ) and ( OpenDlg.FileName <> '' )  then
  163.     RichTextBox.LoadFile(OpenDlg.FileName, RichTextBoxStreamType.PlainText);
  164. end;
  165.  
  166. procedure TDocument.FormatFontClicked(Sender: TObject; Args: EventArgs);
  167. var
  168.   miClicked: TMenuItem;
  169. begin
  170.   miClicked:= TMenuItem(Sender);
  171.   miClicked.Checked:= True;
  172.   miFormatFontChecked.Checked:= False;
  173.   miFormatFontChecked:= miClicked ;
  174.  
  175.   if (miClicked = miSansSerif) then
  176.     currentFontFamily:= sansSerifFontFamily
  177.   else if (miClicked = miSerif) then
  178.     currentFontFamily:= serifFontFamily
  179.   else
  180.     currentFontFamily:= monoSpaceFontFamily;
  181.  
  182.   RichTextBox.Font:= TFont.Create(currentFontFamily, FFontSize);
  183. end;
  184.  
  185. procedure TDocument.FormatSizeClicked(Sender: TObject; Args: EventArgs);
  186. var
  187.   miClicked: TMenuItem;
  188. begin
  189.   miClicked:= TMenuItem(Sender);
  190.   miClicked.Checked:= True;
  191.   miFormatSizeChecked.Checked:= False;
  192.   miFormatSizeChecked:= miClicked;
  193.  
  194.   if (miClicked = miSmall) then
  195.     FFontSize:= FontSizes[Small]
  196.   else if (miClicked = miLarge) then
  197.     FFontSize:= FontSizes[Large]
  198.   else
  199.     FFontSize:= FontSizes[Medium];
  200.  
  201.   RichTextBox.Font:= TFont.Create(currentFontFamily, FFontSize);
  202. end;
  203.  
  204. procedure TDocument.InitComponents;
  205. begin
  206.   SuspendLayout;
  207.  
  208.   FFontSize:= FontSizes[Medium];
  209.   FComponents:= TContainer.Create;
  210.   MainMenu:= TMainMenu.Create;
  211.   RichTextBox:= TRichTextBox.Create;
  212.   RichTextBox.Size:= TSize.Create(292, 273);
  213.   RichTextBox.TabIndex:= 0;
  214.   RichTextBox.Dock:= DockStyle.Fill;
  215.   AutoScaleBaseSize:= TSize.Create(5, 13);
  216.   ClientSize:= TSize.Create(392, 117);
  217.   Menu:= MainMenu;
  218.   Controls.Add(RichTextBox);
  219.  
  220.   ResumeLayout(False);
  221. end;
  222.  
  223. end.
  224.